package top.tagao.m01_structuralPatterns.s_01_proxyDesign.proxy_02_dynamic.dy_01_jdk;

import top.tagao.m01_structuralPatterns.s_01_proxyDesign.proxy_01_static.ClothFactory;
import top.tagao.m01_structuralPatterns.s_01_proxyDesign.proxy_01_static.NikeClothFactory;

import java.lang.reflect.Proxy;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/26 19:02
 */
public class ProxyDynamic {
    public static void main(String[] args) {
        //1.通过反射生成 被代理类的类加载器（Use by: 3-创建代理类的对象）
        ClassLoader classLoader = ISubject.class.getClassLoader();

        //2.生成能处理执行的逻辑的类
        CustomizeHandle customizeHandle = new CustomizeHandle(new ISubjectImpl());
        //3.创建代理类的对象
        ISubject iSubject = (ISubject)Proxy.newProxyInstance(classLoader, new Class[]{ISubject.class}, customizeHandle);
        //4.执行被代理类的方法
        iSubject.execute();

        System.out.println("-------------------------");

        classLoader = ClothFactory.class.getClassLoader();
        NikeClothFactory nikeClothFactory = new NikeClothFactory();
        customizeHandle = new CustomizeHandle(nikeClothFactory);
        ClothFactory clothFactory = (ClothFactory)Proxy.newProxyInstance(classLoader, nikeClothFactory.getClass().getInterfaces(), customizeHandle);
        clothFactory.produceCloth();
    }
}
